﻿using AutoMapper;
using AutoRegistDependency.Attributes;
using Microsoft.Extensions.Logging;
using Models.Models.Sugar;
using Repositories.Base;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Services.Base
{
    [Component]
    public class BaseService<T> : IBaseService<T> where T : class, new()
    {
        protected IBaseRepository<T> repository;
        protected ILogger<T> logger;
        public BaseService(IBaseRepository<T> repository, ILogger<T> logger)
        {
            this.repository = repository;
            this.logger = logger;
        }

        public virtual bool Add(T entity)
        {
            return repository.Add(entity)>0;
        }

        public virtual bool Add(List<T> entities)
        {
            return repository.Add(entities)>0;
        }

        public virtual async Task<bool> AddAsync(T entity)
        {
            return await repository.AddAsync(entity)>0;
        }

        public virtual async Task<bool> AddAsync(List<T> entities)
        {
            return await repository.AddAsync(entities)>0;
        }


        public virtual bool Delete(T entity)
        {
            return repository.Delete(entity)>0;
        }

        public virtual bool Delete(Expression<Func<T, bool>> deleteExpression)
        {
            return repository.Delete(deleteExpression)>0;
        }

        public bool Delete(List<T> entities)
        {
            return repository.Delete(entities) > 0;
        }

        public virtual async Task<bool> DeleteAsync(T entity)
        {
            return await repository.DeleteAsync(entity) > 0;
        }

        public virtual async Task<bool> DeleteAsync(Expression<Func<T, bool>> deleteExpression)
        {
            return await repository.DeleteAsync(deleteExpression) > 0;
        }

        public async Task<bool> DeleteAsync(List<T> entities)
        {
            return await repository.DeleteAsync(entities) > 0;
        }

        public virtual T Find(Expression<Func<T, bool>> findExpression)
        {
            return repository.Find(findExpression);
        }

        public virtual Task<T> FindAsync(Expression<Func<T, bool>> findExpression)
        {
            return repository.FindAsync(findExpression);
        }

        public virtual List<T> GetList(Expression<Func<T, bool>> whereExpression)
        {
            return repository.GetList(whereExpression);
        }

        public virtual List<T> GetList(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, OrderByType orderByType = OrderByType.Asc)
        {
            return repository.GetList(whereExpression, orderExpression, orderByType);
        }

        public virtual Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression)
        {
            return repository.GetListAsync(whereExpression);
        }

        public virtual Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, OrderByType orderByType = OrderByType.Asc)
        {
            return repository.GetListAsync(whereExpression, orderExpression, orderByType);
        }

        public virtual PageModel<T> GetPaged(Expression<Func<T, bool>> whereExpression, int page, int limit)
        {
            return repository.GetPaged(whereExpression, page, limit);
        }

        public virtual PageModel<T> GetPaged(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, OrderByType orderByType, int page, int limit)
        {
            return repository.GetPaged(whereExpression, orderExpression,orderByType, page, limit);
        }

        public virtual Task<PageModel<T>> GetPagedAsync(Expression<Func<T, bool>> whereExpression, int page, int limit)
        {
            return GetPagedAsync(whereExpression, page, limit);
        }

        public virtual Task<PageModel<T>> GetPagedAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, OrderByType orderByType, int page, int limit)
        {
            return repository.GetPagedAsync(whereExpression, page, limit);
        }

        public virtual bool Update(T entity)
        {
            return repository.Update(entity)>0;
        }

        public virtual bool Update(List<T> entities)
        {
            return repository.Update(entities)>0;
        }

        public virtual async Task<bool> UpdateAsync(T entity)
        {
            return await repository.UpdateAsync(entity) > 0;
        }

        public virtual async Task<bool> UpdateAsync(List<T> entities)
        {
            return await repository.UpdateAsync(entities) > 0;
        }

        public virtual bool UpdateExpression(Expression<Func<T, bool>> whereExpression, Expression<Func<T, T>> setColumns)
        {
            return repository.UpdateExpression(whereExpression, setColumns) > 0;
        }

        public virtual bool UpdateExpression(Expression<Func<T, bool>> whereExpression, params Expression<Func<T, bool>>[] setColumn)
        {
            return repository.UpdateExpression(whereExpression, setColumn) > 0;
        }

        public virtual async Task<bool> UpdateExpressionAsync(Expression<Func<T, bool>> whereExpression, Expression<Func<T, T>> setColumns)
        {
            return await repository.UpdateExpressionAsync(whereExpression, setColumns) > 0;
        }

        public virtual async Task<bool> UpdateExpressionAsync(Expression<Func<T, bool>> whereExpression, params Expression<Func<T, bool>>[] setColumn)
        {
            return await repository.UpdateExpressionAsync(whereExpression, setColumn) > 0;
        }
    }
}
